package com.coderside.fragment.thread.ABCD;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;

public class TT {
	static int circleNum = 1;
	final static CountDownLatch end = new CountDownLatch(4 * circleNum);
	final static CountDownLatch start = new CountDownLatch(1);

	/**
	 * @param args
	 * @throws FileNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws FileNotFoundException {
		long a = System.currentTimeMillis();
		ConcurrentLinkedQueue[] queue = new ConcurrentLinkedQueue[5];
		FileOutputStream[] fos = new FileOutputStream[4];

		for (int i = 1; i < 5; i++) {
			queue[i] = new ConcurrentLinkedQueue();
			fos[i - 1] = new FileOutputStream(new File("D:\\" + (char) (i + 64)
					+ ".txt"));
		}
		queue[1].add("1");
		queue[1].add("2");
		queue[1].add("3");
		queue[1].add("4");

		queue[2].add("2");
		queue[2].add("3");
		queue[2].add("4");
		queue[2].add("1");

		queue[3].add("3");
		queue[3].add("4");
		queue[3].add("1");
		queue[3].add("2");

		queue[4].add("4");
		queue[4].add("1");
		queue[4].add("2");
		queue[4].add("3");

		Worker[] w = new Worker[4];
		for (int i = 1; i < 5; i++) {
			w[i - 1] = new Worker(queue, i + "", end, start, fos, circleNum);
			w[i - 1].start();
		}

		start.countDown(); // 熟悉的开闸放狗
		try {
			end.await();
			long b = System.currentTimeMillis();
			System.out.println(b - a);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}
